<?php


namespace App\Http\Controllers\User;


use App\Http\Controllers\BaseController;
use App\Models\Event;
use App\Models\EventEmployee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EmployeesController extends BaseController
{
    /**
     * 员工接受事件 /employee/accept/{event_id}
     * @param $event_id
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function acceptEvent($event_id, Request $request){
        //判断事件是否存在
        if (!$data = $this->isEventExist($event_id)){
            return $this->returnJson(BaseController::NOTFOUND, 'not found');
        }

        //判断用户是否分配
        if (!$this->isUserDistribute($event_id, $request->cache_data['id'])){
            return $this->returnJson(BaseController::UNAUTHORIZED, 'the employee is not assigned to the event');
        }

        //判断事件状态和当前事件员工的状态是否pending
        if (!$this->isEventStatus($event_id, $request->cache_data['id'], 0)) {
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'This event status is not pending');
        }

        //修改当前员工事件状态
        EventEmployee::where([
            'event_id'  =>  $event_id,
            'user_id'   =>  $request->cache_data['id'],
        ])->update(['status' => 1]);

        //查询当前事件所有员工的状态是否accepted, 是则修改当前事件的状态, 否则返回当前事件的状态pending
        $data = EventEmployee::where('event_id', $event_id)->select('status', DB::raw('count(*) as num'))->groupBy('status')->get();

        //还有员工没有接受
        foreach ($data as $value){
            if ($value['status'] == 0){
                return $this->returnJson(BaseController::OK, 'success', [
                    'id'    =>  $event_id,
                    'status'=>  'pending'
                ]);
            }
        }

        //所有员工都已经接受, 修改当前事件状态为accepted
        Event::where('id', $event_id)->update(['status' => 1]);

        return $this->returnJson(BaseController::OK, 'success', [
            'id'    =>  $event_id,
            'status'=>  'accepted'
        ]);
    }

    /**
     * 员工完成事件 /employee/finish/{event_id}
     * @param $event_id
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function finishEvent($event_id, Request $request){
        //判断事件是否存在
        if (!$this->isEventExist($event_id)) {
            return $this->returnJson(BaseController::NOTFOUND, 'not found');
        }

        //当前是管理员可以直接修改事件状态为finished
        if (!$request->cache_data['is_admin']){
            Event::where('id', $event_id)->update(['status' => 2]);
            return $this->returnJson(BaseController::OK, 'success', [
                'id'    =>  $event_id,
                'status'=>  'finished'
            ]);
        }

        //判断用户是否分配
        if (!$this->isUserDistribute($event_id, $request->cache_data['id'])){
            return $this->returnJson(BaseController::UNAUTHORIZED, 'the employee is not assigned to the event');
        }

        //判断事件状态和当前事件员工的状态是否accepted
        if (!$this->isEventStatus($event_id, $request->cache_data['id'], 1)) {
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, "the event's status is not 'accepted'");
        }

        //修改事件状态
        Event::where('id', $event_id)->update(['status' => 2]);
        return $this->returnJson(BaseController::OK, 'success', [
            'id'    =>  $event_id,
            'status'=>  'finished'
        ]);
    }

    /**
     * 判断事件是否存在
     * @param $event_id
     * @return bool
     */
    private function isEventExist($event_id){
        $is_event = Event::where('id', $event_id)->first();
        if (!$is_event) return false;
        return true;
    }

    /**
     * 判断用户是否分配事件
     * @param $event_id
     * @param $user_id
     * @return bool
     */
    private function isUserDistribute($event_id, $user_id){
        $is_distribute = EventEmployee::where([
            'event_id'  =>  $event_id,
            'user_id'   =>  $user_id,
        ])->first();
        if (!$is_distribute) return false;
        return true;
    }

    /**
     * 判断当前事件状态和当前事件员工的状态是否为指定状态
     * @param $event_id
     * @param $user_id
     * @param $status
     * @return bool
     */
    private function isEventStatus($event_id, $user_id, $status){
        //判断当前事件状态
        $is_event_status = Event::where([
            'id' => $event_id,
            'status' => $status
        ])->first();

        //判断当前事件的员工状态
        $is_employee_status = EventEmployee::where([
            'event_id' => $event_id,
            'user_id' => $user_id,
            'status' => $status
        ])->first();

        if (!($is_event_status && $is_employee_status)) return false;
        return true;
    }
}
